﻿using SoundEngine3.AudioProcessing.Filters;
using static ZquenceStudio.DefaultDspPlugins.Effects.Resonance;

namespace DefaultDspPluginsLib.Depricated
{
    public class Effect_Resonance
    {
        FilterResonance[] resonants;

        /// <summary>
        /// Get or set the flanger delta time.
        /// </summary>
        public double Cutoff
        {
            get { return resonants[0].Cutoff; }
            set
            {
                foreach (var resonant in resonants)
                {
                    resonant.Cutoff = value;
                }
            }
        }

        /// <summary>
        /// Get or set the flanger buffer length.
        /// </summary>
        public double Res
        {
            get { return resonants[0].Res; }
            set
            {
                foreach (var resonant in resonants)
                {
                    resonant.Res = value;
                }
            }
        }

        public Effect_Resonance()
        {
            resonants = new FilterResonance[1];

            resonants[0] = new FilterResonance();
            resonants[0].Reset();
        }
        
        public void Setup(int channels, double sampleRate)
        {
            double resonant = this.Res;
            double cutoff = this.Cutoff;

            resonants = new FilterResonance[channels];
            for (int i = channels - 1; i >= 0; i--)
            {
                resonants[i] = new FilterResonance();
                resonants[i].SetBitrate(sampleRate);
            }

            this.Res = resonant;
            this.Cutoff = cutoff;
        }

        public void Process(ref ResonanceData data, double[] wav_in_left, double[] wav_in_right, double[] wav_out_left, double[] wav_out_right)
        {
            if (data.Channels == 1)
            {
                resonants[0].ProcessTime(wav_in_left, wav_out_left);
            }
            else if (data.Channels == 2)
            {
                resonants[0].ProcessTime(wav_in_left, wav_out_left);
                resonants[0].ProcessTime(wav_in_right, wav_out_right);
            }
        }
    }
}
